package com.gsn.chooseRoom;

import java.io.File;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenCallback.EventType;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.FlickScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.GsnImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.gsn.common.SettingGroup;
import com.gsn.dataCommon.ChannelModel;
import com.gsn.dataCommon.ConfigModel;
import com.gsn.dataCommon.UserData;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.GsnException;
import com.gsn.engine.gdx.GsnParticleActor;
import com.gsn.engine.myplay.GsnLayer;
import com.gsn.engine.utility.UtilityGettor;
import com.gsn.engine.utility.IUtility.IPictureDownloadListener;
import com.gsn.name.Sort;
import com.gsn.name.StatePortal;
import com.gsn.poker.asset.FontTexture;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class ChooseRoomLayer extends GsnLayer implements ClickListener, IPictureDownloadListener {

	private static final String tag = "Choose Room Layer";
	PortalTexture portalTexture;
	GsnImageButton playButton;
	GsnImageButton makeButton;
	GsnImageButton updateButton;
	GsnImageButton shopButton;
	GsnImageButton quitButton;

	GsnImageButton maskIndexButton;
	GsnImageButton maskBetButton;
	GsnImageButton maskNumButton;
	GsnImageButton dropButton;

	Image sortDownBet;
	Image sortDownIndex;
	Image sortDownNum;

	Image sortUpBet;
	Image sortUpIndex;
	Image sortUpNum;

	FlickScrollPane scrollChooseRoom;
	Image bgChooseRoom;
	Image bgChooseLeaf;

	Label labelGold;
	Label labelName;
	Label labelXu;
	Label labelJackpot;
	Label labelLeaf;

	ListChooseRoom listChooseRoom;
	ChooseLeaf chooseLeaf;
	Image bg;

	Image avatar;
	private File avatarFile;

	public ChooseRoomLayer(float width, float height) {
		super(width, height);
		portalTexture = PortalTexture.inst();
		init();
	}

	public void init() {
		bg = new Image(portalTexture.bgPortal);
		bg.width = width;
		bg.height = height;
		addActor(bg);

		Image bgAvatar = new Image(portalTexture.bgAvatar);
		portalTexture.scaleActor(bgAvatar);
		addActor(bgAvatar);
		bgAvatar.y = height * 0.97f - bgAvatar.height;

		playButton = new GsnImageButton(portalTexture.iconPlay, portalTexture.iconPlayPress);
		portalTexture.scaleActor(playButton);
		float pad = (width - 4 * playButton.getRealWidth()) / 5;
		addActor(playButton);
		playButton.setClickListener(this);
		playButton.x = pad;

		makeButton = new GsnImageButton(portalTexture.iconMake, portalTexture.iconMakePress);
		portalTexture.scaleActor(makeButton);
		addActor(makeButton);
		makeButton.setClickListener(this);
		makeButton.x = playButton.x + pad + playButton.getRealWidth();

		shopButton = new GsnImageButton(portalTexture.shopButton, portalTexture.shopButtonPress);
		portalTexture.scaleActor(shopButton);
		addActor(shopButton);
		shopButton.setClickListener(this);
		shopButton.x = makeButton.x + pad + makeButton.getRealWidth();

		updateButton = new GsnImageButton(portalTexture.iconUpdate, portalTexture.iconUpdatePress);
		portalTexture.scaleActor(updateButton);
		addActor(updateButton);
		updateButton.setClickListener(this);
		updateButton.x = shopButton.x + pad + shopButton.getRealWidth();

		Image line = new Image(portalTexture.iconLine);
		portalTexture.scaleActor(line);
		addActor(line);
		line.x = width - line.width;
		line.y = height * 0.97f - line.height;

		labelGold = new Label("1000", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		labelName = new Label("Cường cận", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		labelXu = new Label("1000", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));

		pad = (bgAvatar.height - 3 * labelGold.getTextBounds().height) / 4;

		addActor(labelXu);

		Image iconXu = new Image(portalTexture.iconXuSmall);
		portalTexture.scaleActor(iconXu);
		addActor(iconXu);
		iconXu.x = bgAvatar.x + bgAvatar.width * 0.38f;
		iconXu.y = bgAvatar.y + pad;

		labelXu.x = iconXu.x + iconXu.width * 1.25f;
		labelXu.y = bgAvatar.y + pad;

		Image iconGold = new Image(portalTexture.iconGoldSmall);
		portalTexture.scaleActor(iconGold);
		addActor(iconGold);
		iconGold.x = bgAvatar.x + bgAvatar.width * 0.38f;
		iconGold.y = labelXu.y + pad + labelXu.getTextBounds().height;

		addActor(labelGold);
		labelGold.x = iconGold.x + iconGold.width * 1.25f;
		labelGold.y = iconGold.y;

		addActor(labelName);
		labelName.x = labelXu.x;
		labelName.y = labelGold.y + pad + labelName.getTextBounds().height;

		bgChooseRoom = new Image(portalTexture.bgChooseRoom);
		portalTexture.scaleActor(bgChooseRoom);
		addActor(bgChooseRoom);

		pad = (width - bgChooseRoom.width) / 2;
		bgChooseRoom.x = pad;
		bgChooseRoom.y = (bgAvatar.y - playButton.getRealHeight() - playButton.y - bgChooseRoom.height) / 2 + playButton.y + playButton.getRealHeight();

		Image image = new Image(portalTexture.chooseRoomButton);
		portalTexture.scaleActor(image);
		listChooseRoom = new ListChooseRoom(image.width);
		scrollChooseRoom = new FlickScrollPane(listChooseRoom);
		scrollChooseRoom.setupOverscroll(30, 150, 100);
		scrollChooseRoom.width = image.width;

		scrollChooseRoom.height = bgChooseRoom.height * 0.88f;
		addActor(scrollChooseRoom);
		scrollChooseRoom.y = bgChooseRoom.y;
		scrollChooseRoom.x = bgChooseRoom.x + (bgChooseRoom.width - scrollChooseRoom.width) / 2;

		maskIndexButton = new GsnImageButton(portalTexture.maskIndexButton, portalTexture.maskIndexButtonPress);
		portalTexture.scaleActor(maskIndexButton);
		addActor(maskIndexButton);
		maskIndexButton.setClickListener(this);
		maskIndexButton.x = bgChooseRoom.x;
		maskIndexButton.y = bgChooseRoom.y + 0.91f * bgChooseRoom.height;

		sortDownIndex = new Image(portalTexture.sortDown);
		portalTexture.scaleActor(sortDownIndex);
		addActor(sortDownIndex);
		sortDownIndex.x = maskIndexButton.x + maskIndexButton.getRealWidth() - sortDownIndex.width;
		sortDownIndex.y = maskIndexButton.y;

		sortUpIndex = new Image(portalTexture.sortUp);
		portalTexture.scaleActor(sortUpIndex);
		addActor(sortUpIndex);
		sortUpIndex.x = maskIndexButton.x + maskIndexButton.getRealWidth() - sortUpIndex.width;
		sortUpIndex.y = maskIndexButton.y;

		maskBetButton = new GsnImageButton(portalTexture.maskBetButton, portalTexture.maskBetButtonPress);
		portalTexture.scaleActor(maskBetButton);
		addActor(maskBetButton);
		maskBetButton.setClickListener(this);
		maskBetButton.x = bgChooseRoom.x + bgChooseRoom.width * 0.53f;
		maskBetButton.y = maskIndexButton.y;

		sortDownBet = new Image(portalTexture.sortDown);
		portalTexture.scaleActor(sortDownBet);
		addActor(sortDownBet);
		sortDownBet.x = maskBetButton.x + maskBetButton.getRealWidth() - sortDownBet.width;
		sortDownBet.y = maskBetButton.y;

		sortUpBet = new Image(portalTexture.sortUp);
		portalTexture.scaleActor(sortUpBet);
		addActor(sortUpBet);
		sortUpBet.x = maskBetButton.x + maskBetButton.getRealWidth() - sortUpBet.width;
		sortUpBet.y = maskBetButton.y;

		maskNumButton = new GsnImageButton(portalTexture.maskNumButton, portalTexture.maskNumButtonPress);
		portalTexture.scaleActor(maskNumButton);
		addActor(maskNumButton);
		maskNumButton.setClickListener(this);
		maskNumButton.x = bgChooseRoom.x + bgChooseRoom.width * 0.77f;
		maskNumButton.y = maskIndexButton.y;

		sortDownNum = new Image(portalTexture.sortDown);
		portalTexture.scaleActor(sortDownNum);
		addActor(sortDownNum);
		sortDownNum.x = maskNumButton.x + maskNumButton.getRealWidth() - sortDownNum.width;
		sortDownNum.y = maskNumButton.y;

		sortUpNum = new Image(portalTexture.sortUp);
		portalTexture.scaleActor(sortUpNum);
		addActor(sortUpNum);
		sortUpNum.x = maskNumButton.x + maskNumButton.getRealWidth() - sortUpNum.width;
		sortUpNum.y = maskNumButton.y;

		sortUpBet.visible = false;
		sortUpIndex.visible = false;
		sortDownBet.visible = false;
		sortUpNum.visible = false;
		sortDownIndex.visible = false;

		labelJackpot = new Label("Jackpot: 1000000", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelJackpot);

		labelJackpot.x = bgAvatar.x + width * 0.05f + bgAvatar.width;
		labelJackpot.y = height * 0.9f;

		bgChooseLeaf = new Image(portalTexture.bgChooseLeaf);
		portalTexture.scaleActor(bgChooseLeaf);
		addActor(bgChooseLeaf);
		bgChooseLeaf.x = labelJackpot.x;
		bgChooseLeaf.y = bgAvatar.y;

		labelLeaf = new Label("Tá Điền 1: 1/1000", new Label.LabelStyle(FontTexture.inst().fontMedium, new Color(1, 1, 1, 1)));
		addActor(labelLeaf);

		labelLeaf.x = bgChooseLeaf.x;
		labelLeaf.y = bgChooseLeaf.y;

		dropButton = new GsnImageButton(portalTexture.iconMaskDrop, portalTexture.iconMaskDropPress);
		portalTexture.scaleActor(dropButton);
		addActor(dropButton);
		dropButton.setClickListener(this);
		dropButton.x = bgChooseLeaf.x;
		dropButton.y = bgChooseLeaf.y;

		chooseLeaf = new ChooseLeaf();
		chooseLeaf.width = bgChooseLeaf.width;
		chooseLeaf.x = bgChooseLeaf.x;
		addActor(chooseLeaf);
		chooseLeaf.hide();
		updateEnterGame();

		quitButton = new GsnImageButton(portalTexture.quitButton, portalTexture.quitButtonPress);
		portalTexture.scaleActor(quitButton);
		addActor(quitButton);
		quitButton.setClickListener(this);
		quitButton.x = width - quitButton.getRealWidth() * 1.2f;
		quitButton.y = line.y + (line.height - quitButton.getRealHeight()) / 2;

		avatar = new Image();
		addActor(avatar);

		avatar.width = avatar.height = bgAvatar.height * 0.95f;
		avatar.x = bgAvatar.height * 0.025f;
		avatar.y = bgAvatar.y + bgAvatar.height * 0.025f;

	}

	@Override
	public boolean keyDown(int keycode) {

		return super.keyDown(keycode);
	}

	@Override
	public void click(Actor actor, float x, float y) {
		// TODO Auto-generated method stub
		GsnParticleActor par = new GsnParticleActor(PortalTexture.inst().clickEff);
		Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);

		par.setPostition(v.x, v.y);

		par.act(100000);
		addActor(par);
		par.start();
		PokerSound.playClick();
		if (!MyPoker.game.statePortal.equals(StatePortal.CHOOSE_ROOM))
			return;

		if (actor != dropButton) {
			chooseLeaf.hide();
		}
		if (actor == playButton) {

			MyPoker.game.statePortal = StatePortal.CHOOSING_ROOM;
			try {
				MyPoker.game.playScreen.finishInit();
			} catch (GsnException ex) {
				ex.printStackTrace();
			}
			MyPoker.game.chooseRoomScreen.setDefaultInputListener();
			MyPoker.client.send(PacketFactory.playNow());
			UtilityGettor.inst().showProgressDialog("Tìm bàn", "Đang tìm bàn");

		} else if (actor == makeButton) {

			((ChooseRoomScreen) screen).showMakeRoom();

		} else if (actor == updateButton) {

			MyPoker.client.send(PacketFactory.updateListTable());
		} else if (actor == quitButton) {

			MyPoker.game.setChooseChannelScreen();
			MyPoker.client.send(PacketFactory.createExitChannel());
		} else if (actor == shopButton) {

			((ChooseRoomScreen) screen).showShopGold();
		} else if (actor == dropButton) {

			if (!chooseLeaf.visible) {
				chooseLeaf.show();
				chooseLeaf.y = bgChooseLeaf.y - chooseLeaf.height;
			} else {
				chooseLeaf.hide();
			}
		} else if (actor == maskBetButton) {
			updateSortImage();

			if (Math.abs(ConfigModel.sort) == Math.abs(Sort.BET_MAX_MIN)) {
				ConfigModel.sort = -ConfigModel.sort;
				if (ConfigModel.sort == Sort.BET_MAX_MIN) {

					sortDownBet.visible = true;
				} else {
					sortUpBet.visible = true;

				}
			} else {
				ConfigModel.sort = Sort.BET_MAX_MIN;
				sortDownBet.visible = true;
			}
			ConfigModel.sort();
			listChooseRoom.updateRoomNum();
		} else if (actor == maskIndexButton) {
			updateSortImage();

			if (Math.abs(ConfigModel.sort) == Math.abs(Sort.INDEX_MAX_MIN)) {
				ConfigModel.sort = -ConfigModel.sort;
				if (ConfigModel.sort == Sort.INDEX_MAX_MIN) {

					sortDownIndex.visible = true;
				} else {
					sortUpIndex.visible = true;

				}
			} else {
				ConfigModel.sort = Sort.INDEX_MAX_MIN;
				sortDownIndex.visible = true;
			}
			ConfigModel.sort();
			listChooseRoom.updateRoomNum();

		} else if (actor == maskNumButton) {
			updateSortImage();

			if (Math.abs(ConfigModel.sort) == Math.abs(Sort.NUM_MAX_MIN)) {
				ConfigModel.sort = -ConfigModel.sort;
				if (ConfigModel.sort == Sort.NUM_MAX_MIN) {

					sortDownNum.visible = true;
				} else {
					sortUpNum.visible = true;

				}
			} else {
				ConfigModel.sort = Sort.NUM_MAX_MIN;
				sortDownNum.visible = true;
			}
			ConfigModel.sort();
			listChooseRoom.updateRoomNum();
		}

	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {

		if (!super.touchUp(x, y, pointer, button)) {

			chooseLeaf.hide();

		}

		return true;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		// TODO Auto-generated method stub
		// chooseLeaf.hide();
		return super.touchDown(x, y, pointer, button);
	}

	public void updateInfoUser() {
		labelGold.setText(ActorUtility.formatNumber(UserData.gold));
		labelXu.setText(ActorUtility.formatNumber(UserData.xu));
		labelName.setText(ActorUtility.shortString(14, UserData.name));

	}

	public void updateEnterGame() {
		// TODO Auto-generated method stub

		updateInfoUser();
		setAvatar();
		updateListRoom();
		updateJackpot();
		updateLeaf();
	}

	private void setAvatar() {
		// TODO Auto-generated method stub
		UtilityGettor.inst().downloadPictureAsync("me", UserData.avatar, 64, 64, UserData.name + ".png", this);
	}

	public void updateListRoom() {
		listChooseRoom.update();

		/*
		 * if (scrollChooseRoom != null) { scrollChooseRoom.remove(); }
		 * 
		 * Image image = new Image(portalTexture.chooseRoomButton);
		 * portalTexture.scaleActor(image); scrollChooseRoom = new
		 * FlickScrollPane(listChooseRoom); scrollChooseRoom.setupOverscroll(30,
		 * 150, 100); scrollChooseRoom.width = image.width;
		 * 
		 * scrollChooseRoom.height = bgChooseRoom.height * 0.88f;
		 * addActor(scrollChooseRoom); scrollChooseRoom.x = bgChooseRoom.x +
		 * (bgChooseRoom.width - scrollChooseRoom.width) / 2; // chooseRoom
		 */
		if (listChooseRoom.height < 0.88f * bgChooseRoom.height) {
			scrollChooseRoom.y = bgChooseRoom.y + bgChooseRoom.height * 0.88f - listChooseRoom.height;
		} else
			scrollChooseRoom.y = bgChooseRoom.y + bgChooseRoom.height * 0.02f;

	}

	public void showParticle(Actor actor, float x, float y) {
		GsnParticleActor par = new GsnParticleActor(PortalTexture.inst().clickEff);
		Vector2 v = ActorUtility.getCoordinate(actor, x, y, this.root);

		par.setPostition(v.x, v.y);

		par.act(100000);
		addActor(par);
		par.start();
	}

	public void updateJackpot() {
		labelJackpot.setText("Jackpot: " + ActorUtility.formatNumber(ConfigModel.jackpot));
	}

	public void updateLeaf() {
		final ChannelModel current = ConfigModel.getInstance().channelModelList[ConfigModel.getInstance().selectedChannel];
		int numLeaf = ConfigModel.selectedLeaf - current.leafStart;
		String text = current.name + " " + (numLeaf + 1) + "(" + current.personCount[numLeaf] + "/" + ConfigModel.maxPerChannel + ")";
		labelLeaf.setText(text);
	}

	public void updateSortImage() {
		sortDownBet.visible = false;
		sortDownIndex.visible = false;
		sortDownNum.visible = false;

		sortUpBet.visible = false;
		sortUpIndex.visible = false;
		sortUpNum.visible = false;
	}

	@Override
	public void onError(Exception e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onFinishLoading(String id, File outFile) {
		// TODO Auto-generated method stub
		avatarFile = outFile;

		MyPoker.game.runInGLThread(new TweenCallback() {

			@Override
			public void onEvent(EventType eventType, BaseTween source) {
				try {
					Texture texture = new Texture(new FileHandle(avatarFile));
					TextureRegion region = new TextureRegion(texture);
					avatar.setRegion(region);
					avatarFile = null;
					// Image a = new Image(region);
					// addActor(a);
				} catch (Exception e) {
					avatarFile.delete();
					e.printStackTrace();
				} finally {
					avatarFile = null;
				}

			}
		}, 0);
	}
}
